module.exports = function ($scope, $resource, $modal, toaster){

	$scope.currentPage = 1;			//当前页码
	$scope.itemsPerPage = 10;		//每页显示几条;
	$scope.searchform = {
		'statusList': [
			{'name': '全部', 'value': null},
			{'name': '已上架', 'value': '1'},
			{'name': '未上架', 'value': '0'}
		]
	}

	//查询全部已设的商户列表
	$scope.search = function(pageNo){
		var para = {
			'pageNo': pageNo,
			'pageSize': $scope.itemsPerPage,
			'status': $scope.searchform.status,
			'name': $scope.searchform.name
		}
		$resource('/api/ac/pc/packageService/getPackageList', {}, {}).save(para, function(res){
			if(res.errcode === 0){
				$scope.list = res.data.results;
				$scope.totalItems = res.data.totalRecord;
				$scope.currentPage = pageNo;
			}else{
				toaster.error({title: '', body: '查询服务套餐列表失败'});
			}
		});
	}
	$scope.search(1);

	$scope.updatePackage = function(obj) {
		var modalInstance = $modal.open({
			template: require('../views/updatePackage.html'),
			controller: 'updatePackage',
			size: 'lg',
			resolve: {
				obj: function () {
					return obj;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result){
            $scope.search($scope.currentPage);
		});
	}

	$scope.setStatus = function(obj,status){
		var para = {
			'id': obj.id,
			'status': status
		}
		$resource('/api/ac/pc/packageService/setPackageStatus', {}, {}).save(para, function(res){
			if(res.errcode === 0){
				obj.status = status;
				toaster.success({title: '', body: '成功！'});
			}else{
				toaster.error({title: '', body: '失败！'});
			}
		});
	}

	$scope.delete = function(obj){
		if(confirm('确认删除此套餐吗？')){
			$resource('/api/ac/pc/packageService/deletePackage', {}, {}).save({'id': obj.id}, function(res){
				if(res.errcode === 0){
					$scope.search($scope.currentPage);
					toaster.success({title: '', body: '删除成功！'});
				}else{
					toaster.error({title: '', body: '删除失败！'});
				}
			});
		}
	}

};